﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Excel.Entity;
using T.FAS.Runtime.Excel.Service;
using T.FAS.Runtime.Excel.SGSPI;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.Excel.SGPlugin
{
    [SGService]
    public class ImportTaskService : IImportTaskService
    {
        readonly ImportTaskManager importTaskManager = new ImportTaskManager();
        /// <summary>
        /// 导入确认
        /// </summary>
        /// <param name="fileURL"></param>
        public Dictionary<string, object> ImportConfirm(string fileURL, bool isEnableExplainRow, List<ImportDef> importDefs)
        {
            if (string.IsNullOrEmpty(fileURL))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportConfirm.fileURL");
            if (importDefs == null)
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportConfirm.importDefs");
            return importTaskManager.ImportConfirm(fileURL, isEnableExplainRow, importDefs);
        }
        /// <summary>
        /// 下载导入模板
        /// </summary>
        /// <param name="cdoID"></param>
        /// <param name="importDefs"></param>
        [SGService(ServiceID = "FASR-DownloadTemplate")]
        public Dictionary<string, string> DownloadTemplate(string cdoID, bool isEnableExplainRow, bool isEnableUniqueCol, List<ImportDef> importDefs)
        {
            if (string.IsNullOrEmpty(cdoID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.DownloadTemplate.cdoID");
            if (importDefs == null)
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.DownloadTemplate.importDefs");
            return importTaskManager.DownloadTemplate(cdoID, isEnableExplainRow, isEnableUniqueCol, importDefs);
        }
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="cdoID"></param>
        /// <param name="importDefs"></param>
        /// <param name="script"></param>
        /// <param name="maxRows"></param>
        /// <param name="fileURL"></param>
        /// <param name="dataQueryID"></param>
        /// <param name="fileName"></param>
        public ImportTask ImportData(string cdoID, List<ImportDef> importDefs, string script, int maxRows, string fileURL, string dataQueryID, string fileName)
        {
            if (string.IsNullOrEmpty(cdoID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportData.cdoID");
            if (string.IsNullOrEmpty(fileURL))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportData.fileURL");
            if (importDefs == null)
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportData.importDefs");
            ImportParam importParam = new ImportParam();
            importParam.CDOID = cdoID;
            importParam.Script = script;
            importParam.FileURL = fileURL;
            importParam.MaxRow = maxRows;
            importParam.DataQueryID = dataQueryID;
            importParam.FileName = fileName;
            return importTaskManager.ImportData(importParam, importDefs, new List<ImportVariable>());
        }
        /// <summary>
        /// 导入2.0
        /// </summary>
        /// <param name="importParam"></param>
        /// <param name="importDefs"></param>
        public ImportTask ImportDataNew(ImportParam importParam, List<ImportDef> importDefs, List<ImportVariable> importVariables)
        {
            if (importParam == null)
                throw new FasException(ExcelConstCode.InValidParam, "ExportTaskService.ImportDataNew.importParam");
            if (string.IsNullOrEmpty(importParam.CDOID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportDataNew.importParam.CDOID");
            if (string.IsNullOrEmpty(importParam.FileURL))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportDataNew.importParam.FileURL");
            if (importDefs == null)
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ImportDataNew.importDefs");
            return importTaskManager.ImportData(importParam, importDefs, importVariables);
        }
        /// <summary>
        /// 失败重试
        /// </summary>
        /// <param name="importTaskDetail"></param>
        /// <param name="importDefs"></param>
        /// <returns></returns>
        public ImportTask Retry(string taskID, ImportParam importParam, List<ImportDef> importDefs, List<ImportVariable> importVariables)
        {
            if (string.IsNullOrEmpty(taskID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.Retry.taskID");
            if (importParam == null)
                throw new FasException(ExcelConstCode.InValidParam, "ExportTaskService.Retry.importParam");
            if (string.IsNullOrEmpty(importParam.CDOID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.Retry.importParam.CDOID");
            if (string.IsNullOrEmpty(importParam.FileURL))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.Retry.importParam.FileURL");
            if (importDefs == null)
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.Retry.importDefs");
            return importTaskManager.Retry(taskID, importParam, importDefs, importVariables);
        }
        /// <summary>
        /// 查询导入任务列表
        /// </summary>
        /// <param name="cdoID"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public Dictionary<string, object> GetImportTaskList(string cdoID, Dictionary<string, string> filter)
        {
            if (string.IsNullOrEmpty(cdoID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.GetImportTaskList.cdoID");
            return importTaskManager.GetImportTaskList(cdoID, filter);
        }
        /// <summary>
        /// 查询导入任务列表
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public Dictionary<string, object> GetImportTaskDetailList(string taskID, Dictionary<string, string> filter)
        {
            if (string.IsNullOrEmpty(taskID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.GetImportTaskDetailList.taskID");
            return importTaskManager.GetImportTaskDetailList(taskID, filter);
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public Dictionary<string, string> DeleteImportTask(string taskID)
        {
            if (string.IsNullOrEmpty(taskID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.DeleteImportTask.taskID");
            return importTaskManager.DeleteImportTask(taskID);
        }
        /// <summary>
        /// 清空任务
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> ClearImportTask(string cdoID)
        {
            if (string.IsNullOrEmpty(cdoID))
                throw new FasException(ExcelConstCode.InValidParam, "ImportTaskService.ClearImportTask.cdoID");
            return importTaskManager.ClearImportTask(cdoID);
        }
        /// <summary>
        /// 清理任务
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> CleanTask(int days)
        {
            return importTaskManager.CleanTask(days);
        }
    }
}
